'use strict';

const conn = require('../../libs/Conn');
const util = require('../../libs/Utils');
const moment = require('moment');

module.exports = async function (params) {
    if(params.check_date === ''){
        params.check_date =null;
    }
    if(params.rectification_date === ''){
        params.rectification_date = null
    }
    let  updateObj = {
            unit_type:params.unit_type,
            check_result:params.check_result,
            check_details:params.check_details,
            check_date:params.check_date,
            check_type:params.check_type,
            rectification_notice:params.rectification_notice,
            rectification_reply:params.rectification_reply,
            rectification_date:params.rectification_date,
            remarks:params.remarks
        };

    let insertUserSql = ``;
    if(params.user_ids){
        let user_ids=params.user_ids;
        user_ids=user_ids.split(',');
        let deleteSql = `DELETE FROM quality_check_user_middle WHERE check_id = ?`;
        await conn(deleteSql,[params.check_id]);
        for (let user_id in user_ids){
            insertUserSql += `INSERT INTO quality_check_user_middle (check_id, user_id) VALUES ('${params.check_id}', '${user_ids[user_id]}');`;
        }
    }
    let sql =  `UPDATE quality_check_file  SET ? WHERE check_id = ?;`+insertUserSql;
    await conn(sql, [updateObj,params.check_id]);
    return {success: true};
};